﻿using DemountCIM.Framework.DataBase;
using DemountCIM.Framework.DataBase.Table;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using WpfUIDLL.WFControls.NumTextBox;

namespace DemountCIM.Windows
{
    /// <summary>
    /// OptionWindow.xaml 的交互逻辑
    /// </summary>
    public partial class OptionWindow : Window
    {
        private Dictionary<int, List<StackPanel>> controlDic;

        private OptionWindowModel model;
        public OptionWindow()
        {
            InitializeComponent();

            controlDic = new Dictionary<int, List<StackPanel>>();

            model = new OptionWindowModel();

            DataContext = model;

            InitControlDic();

            foreach (var item in controlDic[1])
            {
                this.paramPanel.Children.Add(item);
            }
        }

        public void InitControlDic()
        {
            TbUser user = new TbUser();
            DataBaseContainer.GetInstance().GetUserRepository().GetCurUser(ref user);

            foreach (var module in model.Module)
            {
                List<StackPanel> controls = new List<StackPanel>();

                InsertControl(user, module.OptionParamList, ref controls);

                controlDic.Add(module.RunId, controls);
            }
        }

        /// <summary>
        /// 插入控件
        /// </summary>
        public void InsertControl(TbUser user, List<OptionWindowModel.OptionParam> optionParams, ref List<StackPanel> controls)
        {
            foreach (var option in optionParams)
            {
                // 是否可以修改
                bool adapted = user.Level <= option.UserLevel;


                // 容器
                StackPanel stack = new StackPanel();
                stack.Margin = new Thickness(0, 0, 0, 5);

                // 描述
                Label des = new Label();
                des.Foreground = Brushes.Gray;
                var txt = new TextBlock();
                txt.TextWrapping = TextWrapping.Wrap;
                txt.Text = option.Description;
                des.Content = txt;

                Border border = new Border();
                border.BorderBrush = Brushes.LightGray;
                border.BorderThickness = new Thickness(0, 0, 0, 0.4);

                switch (option.ParamType)
                {
                    case ParamType.Int:
                        {
                            Grid grid = new Grid();


                            // 名称
                            Label label = new Label();
                            label.Content = option.ParamName;
                            grid.Children.Add(label);

                            var text = new NumericBox();
                            text.DataContext = option;
                            text.IsEnabled = adapted;
                            text.Width = 100;
                            text.VerticalContentAlignment = VerticalAlignment.Center;
                            text.HorizontalAlignment = HorizontalAlignment.Right;
                            text.SetBinding(NumericBox.TextProperty, new Binding("ParamValue"));
                            grid.Children.Add(text);

                            stack.Children.Add(grid);
                            stack.Children.Add(des);
                            break;
                        }
                    case ParamType.Bool:
                        {

                            var check = new CheckBox();
                            check.VerticalAlignment = VerticalAlignment.Center;
                            check.DataContext = option;
                            check.IsEnabled = adapted;
                            check.SetBinding(CheckBox.ContentProperty, new Binding("ParamName"));
                            check.SetBinding(CheckBox.IsCheckedProperty, new Binding("ParamValue"));

                            stack.Children.Add(check);
                            stack.Children.Add(des);


                            break;
                        }
                    default:
                        {
                            Grid grid = new Grid();

                            // 名称
                            Label label = new Label();
                            label.Content = option.ParamName;
                            grid.Children.Add(label);

                            var text = new TextBox();
                            text.DataContext = option;
                            text.IsEnabled = adapted;
                            text.Width = 300;
                            text.VerticalContentAlignment = VerticalAlignment.Center;
                            text.HorizontalAlignment = HorizontalAlignment.Right;
                            text.SetBinding(TextBox.TextProperty, new Binding("ParamValue"));
                            grid.Children.Add(text);

                            stack.Children.Add(grid);
                            stack.Children.Add(des);

                        }
                        break;
                }

                stack.Children.Add(border);

                controls.Add(stack);
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void RadioButton_Checked(object sender, RoutedEventArgs e)
        {
            int runId = Convert.ToInt16((sender as RadioButton).Tag);

            this.paramPanel.Children.Clear();
            foreach (var item in controlDic[runId])
            {
                this.paramPanel.Children.Add(item);
            }
        }
    }
}
